/*
 * Copyright (c) 2002-2004, Inversoft, All Rights Reserved
 *
 * This software is distribuable under the GNU Lesser General Public License.
 * For more information visit gnu.org.
 */
package org.emergent.antbite.savant.ant.taskdefs;


import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;


/**
 * <p>
 * This class is a task that allows for J2EE applications to
 * be clean of artifacts previously copied over.
 * </p>
 *
 * @author  Brian Pontarelli
 */
public class J2EEArtifactCleanTask extends Task {

    private File todir;
    private List ears = new ArrayList();


    public File getTodir() {
        return todir;
    }

    public void setTodir(File todir) {
        this.todir = todir;
    }

    /**
     * Adds a new configured EAR, which is an ArtifactCopyTask.
     *
     * @param   ear The new EAR to add
     */
    public void addConfiguredEar(Ear ear) {
        String name = ear.getName();
        if (name == null) {
            throw new BuildException("All ear elements must be named");
        }

        File earDir = new File(todir, name);
        ear.setTodir(earDir);
        ear.setFlatten(true);
        ears.add(ear);
    }

    /**
     * Performs the deployment.
     */
    public void execute() {
        for (int i = 0; i < ears.size(); i++) {
            ArtifactUncopyTask act = (ArtifactUncopyTask) ears.get(i);
            act.perform();
        }
    }


    public static class Ear extends ArtifactUncopyTask {

        private String name;
        private List wars = new ArrayList();

        /**
         * Returns the name of this EAR.
         */
        public String getName() {
            return name;
        }

        /**
         * Sets the name of this EAR.
         */
        public void setName(String name) {
            this.name = name;
        }

        public void addConfiguredWar(War war) {
            String name = war.getName();
            if (name == null) {
                throw new BuildException("All war elements must be named");
            }

            war.setFlatten(true);
            wars.add(war);
        }

        /**
         * Performs the deployment.
         */
        public void execute() {
            for (int i = 0; i < wars.size(); i++) {
                War war = (War) wars.get(i);
                String warPath = war.getName() + File.separator + "WEB-INF" +
                    File.separator + "lib";
                File warDir = new File(super.destDir, warPath);

                log("Path for EAR " + super.destDir.getAbsolutePath());
                war.setTodir(warDir);
                war.perform();
            }
        }
    }

    public static class War extends ArtifactUncopyTask {

        private String name;

        /**
         * Returns the name of this WAR.
         */
        public String getName() {
            return name;
        }

        /**
         * Sets the name of this WAR.
         */
        public void setName(String name) {
            this.name = name;
        }
    }
}
